#include "communicate.h"
int send_url(int netfd,char* url){
    train_t train;
    bzero(&train,sizeof(train));
    train.type=SEND_URL;
    train.length=strlen(url);
    memcpy(train.data,url,train.length);
    send(netfd,&train,sizeof(train.type)+sizeof(train.length)+train.length,0);
    return 0;
}
int send_file_dir(int netfd,char *dir){
    train_t train;
    bzero(&train,sizeof(train));
    train.type=SEND_FILE_DIR;
    train.length=strlen(dir);
    memcpy(train.data,dir,train.length);
    send(netfd,&train,sizeof(train.type)+sizeof(train.length)+train.length,0);
    return 0;
}
int recvn(int netfd,void *buf,int length){
    int count=0;                                                                                            
    int recv_size;
    char * temp_buf=(char*)buf;
    while(count<length){
        recv_size=recv(netfd,temp_buf,length-count,0);
        if(recv_size<=0){
            /* return (recv_size==0)?count:-1; */
            return -1;
        }
        count+=recv_size;
        temp_buf+=recv_size;
    }
    return count;
}
int send_tips(int netfd,const char * tips){
    train_t  train;
    bzero(&train,sizeof(train));
    train.type=SEND_TIP;
    train.length=strlen(tips);
    memcpy(train.data,tips,train.length);
    send(netfd,&train,sizeof(train.type)+sizeof(train.length)+train.length,0);
    return 0;
}
